<?php
require_once '../model/Customer.php';
$customer = new Customer();
$arrResult = array();
$action = $customer->processData($_POST['action']);
if($action == "save_customer"){
    $arrData = array(
        'customer_name' => $customer->processData($_POST['customer_name']),
        'phone' => $customer->processData($_POST['phone']),
        'email' => $customer->processData($_POST['email']),
        'address' => $customer->processData($_POST['address']),
        'information' => $customer->processData($_POST['information'])
    );
    $arrResult['customer_id'] = $customer_id = $customer->saveCustomer($arrData);
    
}elseif($action == "edit_customer"){
    $arrData = array(
        'customer_id' => (int) $_POST['customer_id'],
        'customer_name' => $customer->processData($_POST['customer_name']),
        'phone' => $customer->processData($_POST['phone']),
        'email' => $customer->processData($_POST['email']),
        'address' => $customer->processData($_POST['address']),
        'information' => $customer->processData($_POST['information'])
    );
    $rs = $customer->editCustomer($arrData);
    $arrResult['result'] = $rs ;
    
}
elseif($action == "save_sodo"){
    $arrData = array(
        'customer_id' => (int) $_POST['customer_id'],
        'vong_nguc' => (int) $_POST['vong_nguc'],'vong_eo' => (int) $_POST['vong_eo'],'vong_mong' => (int) $_POST['vong_mong'],'ha_nguc' => (int) $_POST['ha_nguc'],
        'ha_eo' => (int) $_POST['ha_eo'],'rong_vai' => (int) $_POST['rong_vai'],'dai_tay_ao' => (int) $_POST['dai_tay_ao'],'dai_ao' => (int) $_POST['dai_ao'],
        'vong_co' => (int) $_POST['vong_co'],'vong_nach' => (int) $_POST['vong_nach'],'bap_tay_tren' => (int) $_POST['bap_tay_tren'],'bap_tay_duoi' => (int) $_POST['bap_tay_duoi'],
        'vong_day' => (int) $_POST['vong_day'],'vong_nguc_tren' => (int) $_POST['vong_nguc_tren'],'vong_nguc_duoi' => (int) $_POST['vong_nguc_duoi'],'dai_quan' => (int) $_POST['dai_quan']
    );
    if($arrData['customer_id'] > 0){
        $arrResult['result'] = $customer->saveSoDo($arrData);
    }
}elseif($action == "save_order"){     
    $date_giao = strtotime($_POST['date_giao']);   
    $date_tra = ($_POST['date_tra']!='') ? strtotime($_POST['date_tra']) : "";    
    $arrData = array(
        'customer_id' => (int) $_POST['customer_id'],
        'order_no' => $_POST['order_no'],'method_id' => (int) $_POST['method_id'],'total_product' => (int) $_POST['total_product'],
        'total_price' => (int) $_POST['total_price'],'description' => $_POST['description'],'date_tra' => $date_tra,
        'date_giao' => $date_giao,'order_status' => (int) $_POST['order_status'],'payment_id'=>(int) $_POST['payment_id']
    );
    if($arrData['customer_id'] > 0){
        $arrResult['order_id'] = $customer->saveOrder($arrData);
    }
}elseif($action == "edit_order"){     
    $date_giao = strtotime($_POST['date_giao']);   
    $date_tra = ($_POST['date_tra']!='') ? strtotime($_POST['date_tra']) : "";    
    $arrData = array(
        'customer_id' => (int) $_POST['customer_id'],'order_id' => (int) $_POST['order_id'],
        'order_no' => $_POST['order_no'],'method_id' => (int) $_POST['method_id'],'total_product' => (int) $_POST['total_product'],
        'total_price' => (int) $_POST['total_price'],'description' => $_POST['description'],'date_tra' => $date_tra,
        'date_giao' => $date_giao,'order_status' => (int) $_POST['order_status'],'payment_id'=>(int) $_POST['payment_id']
    );
    if($arrData['order_id'] > 0){
        $rs = $customer->editOrder($arrData);
        $arrResult['result'] = $rs;
    }
}
elseif($action == "save_order_detail"){          
    $arrData = array(
        'order_id' => (int) $_POST['order_id'],
        'product_code' => $_POST['product_code'],'number_product' => (int) $_POST['number_product'],'price' => (int) $_POST['price'],
        'total_price' => (int) $_POST['total_price'],'url_images' => $_POST['url_images']
        ,'mau_id' => (int) $_POST['mau_id'],'kieu_id'=>(int) $_POST['kieu_id'],'size_id'=>(int) $_POST['size_id']
    );
    if($arrData['order_id'] > 0){
        $arrResult['detail_id'] = $customer->saveOrderDetail($arrData);
    }
}
elseif($action == "delete_detail_order"){          
    $rs = mysql_query("DELETE FROM order_detail WHERE detail_id = ".$_POST['detail_id']);
    if($rs == true){
        $arrResult['result'] = $rs;
    }
}elseif($action == "delete_customer"){          
    $rs = mysql_query("UPDATE customer SET status = 0,update_time= UNIX_TIMESTAMP(NOW()) WHERE customer_id = ".$_POST['customer_id']);
    if($rs == true){
        $rs = mysql_query("UPDATE orders SET status = 0,update_time= UNIX_TIMESTAMP(NOW()) WHERE customer_id = ".$_POST['customer_id']);
        $arrResult['result'] = $rs;
    }
}elseif($action == "delete_orders"){          
    $rs = mysql_query("UPDATE orders SET status = 0,update_time= UNIX_TIMESTAMP(NOW()) WHERE order_id = ".$_POST['order_id']);
    if($rs == true){
        $rs = mysql_query("UPDATE order_detail SET status = 0,update_time= UNIX_TIMESTAMP(NOW()) WHERE order_id = ".$_POST['order_id']);
        $arrResult['result'] = $rs;
    }
}
echo json_encode($arrResult);
?>